﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
    [Serializable()]
    public class KBHTHHCPAPBO : Csla.BusinessBase<KBHTHHCPAPBO>
    {
        #region Business Properties

        private int? _KBHTHHCPAPID = null;
        public int? KBHTHHCPAPID
        {
            get { return this._KBHTHHCPAPID; }
            set { this._KBHTHHCPAPID = value; PropertyHasChanged("KBHTHHCPAPID"); }
        }

        private int? _KBDieuTriID = null;
        public int? KBDieuTriID
        {
            get { return this._KBDieuTriID; }
            set { this._KBDieuTriID = value; PropertyHasChanged("KBDieuTriID"); }
        }

        private int? _KBYLenhID = null;
        public int? KBYLenhID
        {
            get { return this._KBYLenhID; }
            set { this._KBYLenhID = value; PropertyHasChanged("KBYLenhID"); }
        }

        private int? _BenhNhanID = null;
        public int? BenhNhanID
        {
            get { return this._BenhNhanID; }
            set { this._BenhNhanID = value; PropertyHasChanged("BenhNhanID"); }
        }

        private DateTime? _NgayBatDau = null;
        public DateTime? NgayBatDau
        {
            get { return this._NgayBatDau; }
            set { this._NgayBatDau = value; PropertyHasChanged("NgayBatDau"); }
        }

        private DateTime? _NgayKetThuc = null;
        public DateTime? NgayKetThuc
        {
            get { return this._NgayKetThuc; }
            set { this._NgayKetThuc = value; PropertyHasChanged("NgayKetThuc"); }
        }

        private double? _Oxy = null;
        public double? Oxy
        {
            get { return this._Oxy; }
            set { this._Oxy = value; PropertyHasChanged("Oxy"); }
        }

        private double? _KhiTroi = null;
        public double? KhiTroi
        {
            get { return this._KhiTroi; }
            set { this._KhiTroi = value; PropertyHasChanged("KhiTroi"); }
        }

        private double? _FiO2 = null;
        public double? FiO2
        {
            get { return this._FiO2; }
            set { this._FiO2 = value; PropertyHasChanged("FiO2"); }
        }

        private double? _PEEP = null;
        public double? PEEP
        {
            get { return this._PEEP; }
            set { this._PEEP = value; PropertyHasChanged("PEEP"); }
        }

        private double? _ThoiGian = 0;
        public double? ThoiGian
        {
            get { return this._ThoiGian; }
            set { this._ThoiGian = value; PropertyHasChanged("ThoiGian"); }
        }

        private double? _GiaTien = 0;
        public double? GiaTien
        {
            get { return this._GiaTien; }
            set { this._GiaTien = value; PropertyHasChanged("GiaTien"); }
        }

        private int? _UserCreated = Session.LoginID;
        public int? UserCreated
        {
            get { return this._UserCreated; }
            set { this._UserCreated = value; PropertyHasChanged("UserCreated"); }
        }

        private DateTime? _DateCreated = DateTime.MaxValue;
        public DateTime? DateCreated
        {
            get { return this._DateCreated; }
            set { this._DateCreated = value; PropertyHasChanged("DateCreated"); }
        }

        private int? _UserModify = null;
        public int? UserModify
        {
            get { return this._UserModify; }
            set { this._UserModify = value; PropertyHasChanged("UserModify"); }
        }

        private DateTime? _DateModify = null;
        public DateTime? DateModify
        {
            get { return this._DateModify; }
            set { this._DateModify = value; PropertyHasChanged("DateModify"); }
        }

        private bool? _Deleted = false;
        public bool? Deleted
        {
            get { return this._Deleted; }
            set { this._Deleted = value; PropertyHasChanged("Deleted"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _KBHTHHCPAPID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public void SetIsChild()
        {
            MarkAsChild();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newKBHTHHCPAPID = -1;
        private KBHTHHCPAPBO()
        {
            _KBHTHHCPAPID = newKBHTHHCPAPID--;
        }

        public KBHTHHCPAPBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public KBHTHHCPAPBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static KBHTHHCPAPBO NewRootKBHTHHCPAPBO()
        {
            return DataPortal.Create<KBHTHHCPAPBO>(new RootCriteria());
        }

        internal static KBHTHHCPAPBO NewChildKBHTHHCPAPBO()
        {
            return DataPortal.Create<KBHTHHCPAPBO>(new ChildCriteria());
        }

        public static KBHTHHCPAPBO GetRootKBHTHHCPAPBO(int _KBHTHHCPAPID)
        {
            return DataPortal.Fetch<KBHTHHCPAPBO>(new RootCriteria(_KBHTHHCPAPID));
        }

        public static KBHTHHCPAPBO GetRootKBHTHHCPAPBOByWhere(string where)
        {
            return DataPortal.Fetch<KBHTHHCPAPBO>(where);
        }

        internal static KBHTHHCPAPBO GetChildKBHTHHCPAPBO(SqlDataReader dataReader)
        {
            return new KBHTHHCPAPBO(dataReader);
        }

        public static void DeleteKBHTHHCPAPBO(int _KBHTHHCPAPID)
        {
            DataPortal.Delete(new RootCriteria(_KBHTHHCPAPID));
        }

        public static void DeleteKBHTHHCPAPBO(int _KBHTHHCPAPID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_KBHTHHCPAPID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int KBHTHHCPAPID;
            public bool directDelete = false;
            public RootCriteria(int _KBHTHHCPAPID)
            {
                this.KBHTHHCPAPID = _KBHTHHCPAPID;
                this.directDelete = false;
            }

            public RootCriteria(int _KBHTHHCPAPID, bool directDelete)
            {
                this.KBHTHHCPAPID = _KBHTHHCPAPID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int KBHTHHCPAPID;
            public bool directDelete = false;
            public ChildCriteria(int _KBHTHHCPAPID)
            {
                this.KBHTHHCPAPID = _KBHTHHCPAPID;
                this.directDelete = false;
            }

            public ChildCriteria(int _KBHTHHCPAPID, bool directDelete)
            {
                this.KBHTHHCPAPID = _KBHTHHCPAPID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBHTHHCPAPBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[KBHTHHCPAPID] = " + criteria.KBHTHHCPAPID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBHTHHCPAPBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["KBHTHHCPAPID"] is DBNull))
                this._KBHTHHCPAPID = (int)dataReader["KBHTHHCPAPID"];
            if (!(dataReader["KBDieuTriID"] is DBNull))
                this._KBDieuTriID = (int)dataReader["KBDieuTriID"];
            if (!(dataReader["KBYLenhID"] is DBNull))
                this._KBYLenhID = (int)dataReader["KBYLenhID"];
            if (!(dataReader["BenhNhanID"] is DBNull))
                this._BenhNhanID = (int)dataReader["BenhNhanID"];
            if (!(dataReader["NgayBatDau"] is DBNull))
                this._NgayBatDau = (DateTime)dataReader["NgayBatDau"];
            if (!(dataReader["NgayKetThuc"] is DBNull))
                this._NgayKetThuc = (DateTime)dataReader["NgayKetThuc"];
            if (!(dataReader["Oxy"] is DBNull))
                this._Oxy = (double)dataReader["Oxy"];
            if (!(dataReader["KhiTroi"] is DBNull))
                this._KhiTroi = (double)dataReader["KhiTroi"];
            if (!(dataReader["FiO2"] is DBNull))
                this._FiO2 = (double)dataReader["FiO2"];
            if (!(dataReader["PEEP"] is DBNull))
                this._PEEP = (double)dataReader["PEEP"];
            if (!(dataReader["ThoiGian"] is DBNull))
                this._ThoiGian = (double)dataReader["ThoiGian"];
            if (!(dataReader["GiaTien"] is DBNull))
                this._GiaTien = (double)dataReader["GiaTien"];
            if (!(dataReader["UserCreated"] is DBNull))
                this._UserCreated = (int)dataReader["UserCreated"];
            if (!(dataReader["DateCreated"] is DBNull))
                this._DateCreated = (DateTime)dataReader["DateCreated"];
            if (!(dataReader["UserModify"] is DBNull))
                this._UserModify = (int)dataReader["UserModify"];
            if (!(dataReader["DateModify"] is DBNull))
                this._DateModify = (DateTime)dataReader["DateModify"];
            if (!(dataReader["Deleted"] is DBNull))
                this._Deleted = (bool)dataReader["Deleted"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._UserCreated = Session.LoginID;
            this._DateCreated = DateTime.MaxValue;
            this._UserModify = null;
            this._DateModify = null;
            this._Deleted = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBHTHHCPAPBO_InsertBO");
            db.AddInParameter(cmd, "@KBDieuTriID", SqlDbType.Int, this._KBDieuTriID);
            db.AddInParameter(cmd, "@KBYLenhID", SqlDbType.Int, this._KBYLenhID);
            db.AddInParameter(cmd, "@BenhNhanID", SqlDbType.Int, this._BenhNhanID);
            db.AddInParameter(cmd, "@NgayBatDau", SqlDbType.DateTime, this._NgayBatDau);
            db.AddInParameter(cmd, "@NgayKetThuc", SqlDbType.DateTime, this._NgayKetThuc);
            db.AddInParameter(cmd, "@Oxy", SqlDbType.Float, this._Oxy);
            db.AddInParameter(cmd, "@KhiTroi", SqlDbType.Float, this._KhiTroi);
            db.AddInParameter(cmd, "@FiO2", SqlDbType.Float, this._FiO2);
            db.AddInParameter(cmd, "@PEEP", SqlDbType.Float, this._PEEP);
            db.AddInParameter(cmd, "@ThoiGian", SqlDbType.Float, this._ThoiGian);
            db.AddInParameter(cmd, "@GiaTien", SqlDbType.Float, this._GiaTien);
            db.AddInParameter(cmd, "@UserCreated", SqlDbType.Int, this._UserCreated);
            db.AddOutParameter(cmd, "@NewKBHTHHCPAPID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDateCreated", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._KBHTHHCPAPID = (int)cmd.Parameters["@NewKBHTHHCPAPID"].Value;
            this._DateCreated = (DateTime)cmd.Parameters["@NewDateCreated"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._UserModify = Session.LoginID;
            this._DateModify = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBHTHHCPAPBO_UpdateBO");
            db.AddInParameter(cmd, "@KBHTHHCPAPID", SqlDbType.Int, this._KBHTHHCPAPID);
            db.AddInParameter(cmd, "@KBDieuTriID", SqlDbType.Int, this._KBDieuTriID);
            db.AddInParameter(cmd, "@KBYLenhID", SqlDbType.Int, this._KBYLenhID);
            db.AddInParameter(cmd, "@BenhNhanID", SqlDbType.Int, this._BenhNhanID);
            db.AddInParameter(cmd, "@NgayBatDau", SqlDbType.DateTime, this._NgayBatDau);
            db.AddInParameter(cmd, "@NgayKetThuc", SqlDbType.DateTime, this._NgayKetThuc);
            db.AddInParameter(cmd, "@Oxy", SqlDbType.Float, this._Oxy);
            db.AddInParameter(cmd, "@KhiTroi", SqlDbType.Float, this._KhiTroi);
            db.AddInParameter(cmd, "@FiO2", SqlDbType.Float, this._FiO2);
            db.AddInParameter(cmd, "@PEEP", SqlDbType.Float, this._PEEP);
            db.AddInParameter(cmd, "@ThoiGian", SqlDbType.Float, this._ThoiGian);
            db.AddInParameter(cmd, "@GiaTien", SqlDbType.Float, this._GiaTien);
            db.AddInParameter(cmd, "@UserModify", SqlDbType.Int, this._UserModify);
            db.AddInParameter(cmd, "@Deleted", SqlDbType.Bit, this._Deleted);

            db.AddOutParameter(cmd, "@NewDateModify", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DateModify = (DateTime)cmd.Parameters["@NewDateModify"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_KBHTHHCPAPID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBHTHHCPAPBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@KBHTHHCPAPID", SqlDbType.Int, criteria.KBHTHHCPAPID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_KBHTHHCPAPID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
